﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Sector7.Framework.Core;
using Sector7.Framework.Enums;
using Sector7.Framework.Utility;
using Sector7.Framework.DataDictionary.CORS;
using Sector7.Framework.DataDictionary.Utility;
using Sector7.Framework.DataDictionary.Agents;

namespace Sector7.Framework.DataDictionary.Windows
{
    /// <summary>
    /// ValueTypeSummaryWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ValueTypeSummaryWindow : UserControl
    {
        private List<S7_ValueType> m_types = null;

        public event EventHandler GetSuccess = null;

        public ValueTypeSummaryWindow()
        {
            InitializeComponent();
        }

        private void GetData()
        {
            m_types = ValueTypeServiceAgent.GetAll();

            ClientUtil.LoadItemsSource(dtgMain, m_types);

            OnGetSuccess(new EventArgs());
        }

        private void DoModify()
        {
            var valueType = dtgMain.SelectedItem as S7_ValueType;

            if (valueType != null)
            {
                var modifyWindow = new AddValueTypeWindow();
                modifyWindow.ValueType = valueType;
                modifyWindow.InitializeWindow(CrudType.Update);
                modifyWindow.AddSuccess += new EventHandler(AddWindow_AddSuccess);

                modifyWindow.ShowDialog();
            }
        }

        private void btnAddType_Click(object sender, RoutedEventArgs e)
        {
            AddValueTypeWindow addWindow = new AddValueTypeWindow();
            addWindow.InitializeWindow(CrudType.Create);
            addWindow.AddSuccess += new EventHandler(AddWindow_AddSuccess);

            addWindow.ShowDialog();
        }

        private void btnModify_Click(object sender, RoutedEventArgs e)
        {
            DoModify();
        }

        private void AddWindow_AddSuccess(object sender, EventArgs e)
        {
            AddValueTypeWindow addTypeWindow = sender as AddValueTypeWindow;
            
            var type = addTypeWindow.ValueType;
            m_types.Add(type);
            ClientUtil.LoadSelectedItem(dtgMain, type);

            addTypeWindow.ResetWindow();
        }

        protected virtual void OnGetSuccess(EventArgs e)
        {
            if (GetSuccess != null)
                GetSuccess(this, e);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            GetData();
        }

        private void dtgMain_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            DoModify();
        }
    }
}
